package com.lottery.material.controller.admin.circle;

import com.lottery.material.common.EnumInfo;
import com.lottery.material.controller.base.BaseController;
import com.lottery.material.enumeration.ResultEnum;
import com.lottery.material.enumeration.circle.CircleMemberInfoEnum;
import com.lottery.material.pojo.circle.CircleMemberInfo;
import com.lottery.material.pojo.page.Page;
import com.lottery.material.pojo.page.PageData;
import com.lottery.material.pojocustom.circle.CircleMemberInfoCustom;
import com.lottery.material.service.admin.circle.CircleMemberInfoService;
import com.lottery.material.util.EnumUtil;
import com.lottery.material.util.Jurisdiction;
import com.lottery.material.util.PdUtil;
import com.lottery.material.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * Description：圈子成员Controller
 * Created by 朱正磊 on 2019-04-13 20:16
 */
@Controller("adminCircleMemberInfoController")
@RequestMapping("/admin/circleMemberInfo")
public class CircleMemberInfoController extends BaseController {

    //菜单地址，用于权限控制
    private String menuUrl = "admin/circleMemberInfo/list";

    private final CircleMemberInfoService circleMemberInfoService;

    @Autowired
    public CircleMemberInfoController(CircleMemberInfoService circleMemberInfoService) {
        this.circleMemberInfoService = circleMemberInfoService;
    }

    /**
     * description：分页列表
     * user 朱正磊
     * time 2019-04-17 15:06
     * @param page 分页对象
     * @param map 响应数据Map
     * @return 返回模型视图
     */
    @RequestMapping("/list")
    public ModelAndView listPage(Page page, Map<String, Object> map) {
        //分页对象设置页面请求参数
        PageData pd = getPageData();
        PdUtil.rePut(pd);
        page.setPd(pd);

        //查询分页列表
        List<CircleMemberInfoCustom> list = circleMemberInfoService.findCustomListPage(page);
        map.put("list", list);
        map.put("pd", pd);
        map.put("QX", Jurisdiction.getHC());

        //是否禁言（1：否；2：是）
        List<EnumInfo> isBanList = EnumUtil
                .getEnumInfoList(CircleMemberInfoEnum.IS_BAN, CircleMemberInfoEnum.class);
        map.put("isBanList", isBanList);

        //圈内等级（1：普通圈员/预备圈员；2：正式圈员/认证圈员；3：副圈主；4：圈主）
        List<EnumInfo> rankList = EnumUtil
                .getEnumInfoList(CircleMemberInfoEnum.RANK, CircleMemberInfoEnum.class);
        map.put("rankList", rankList);

        return new ModelAndView("circle/circleMemberInfo/listPage", map);
    }

    /**
     * description：详情页面
     * user 朱正磊
     * time 2019-04-17 16:20
     * @param id 主键
     * @param map 响应数据Map
     * @return 返回模型视图
     */
    @RequestMapping("/detailPage")
    public ModelAndView detailPage(Integer id, Map<String, Object> map) {
        if(!Jurisdiction.buttonJurisdiction(menuUrl, "cha")) {//判断是否有查看权限
            return null;
        }

        //根据主键查询扩展类
        CircleMemberInfoCustom circleMemberInfo = circleMemberInfoService.findCustomById(id);
        map.put("circleMemberInfo", circleMemberInfo);

        return new ModelAndView("circle/circleMemberInfo/detailPage", map);
    }

    /**
     * description：修改页面
     * user 朱正磊
     * time 2019-04-17 16:48
     * @param id 主键
     * @param map 响应数据Map
     * @return 返回模型视图
     */
    @RequestMapping("/editPage")
    public ModelAndView editPage(Integer id, Map<String, Object> map) {
        if(!Jurisdiction.buttonJurisdiction(menuUrl, "edit")) {//判断是否有修改权限
            return null;
        }

        //根据主键查询扩展类
        CircleMemberInfoCustom circleMemberInfo = circleMemberInfoService.findCustomById(id);
        map.put("circleMemberInfo", circleMemberInfo);

        //是否禁言（1：否；2：是）
        List<EnumInfo> isBanList = EnumUtil.getEnumInfoList(CircleMemberInfoEnum.IS_BAN, CircleMemberInfoEnum.class);
        map.put("isBanList", isBanList);

        //圈内等级（1：普通圈员/预备圈员；2：正式圈员/认证圈员；3：副圈主；4：圈主）
        List<EnumInfo> rankList = EnumUtil.getEnumInfoList(CircleMemberInfoEnum.RANK, CircleMemberInfoEnum.class);
        rankList.remove(3); //去除圈主
        map.put("rankList", rankList);

        //修改请求地址
        map.put("actionUrl", "admin/circleMemberInfo/edit");

        return new ModelAndView("circle/circleMemberInfo/editPage", map);
    }

    /**
     * description：修改
     * user 朱正磊
     * time 2019-04-17 16:58
     * @param circleMemberInfo 对象
     * @param map 响应数据Map
     * @return 返回模型视图
     */
    @RequestMapping("/edit")
    public ModelAndView edit(CircleMemberInfo circleMemberInfo, Map<String, Object> map) {
        Integer flag = circleMemberInfoService.updateById(circleMemberInfo);
        if (Objects.equals(flag, 0)) { //修改失败
            throw new RuntimeException(ResultEnum.STATUS_PERSISTENT_OPERATE_SQL_FAIL.getMessage());
        } else {
            map.put("msg","success");
            return new ModelAndView("save_result", map);
        }
    }

    /**
     * description：禁言或者取消禁言
     * user 朱正磊
     * time 2019-04-25 21:03
     * @param id 圈员ID
     * @param isBan 是否禁言（1：否；2：是）
     * @return 返回响应对象
     */
    @RequestMapping("/banOrNot")
    @ResponseBody
    public ResponseResult banOrNot(Integer id, Integer isBan) {
        if(!Jurisdiction.buttonJurisdiction(menuUrl, "edit")) {//判断是否有修改权限
            return null;
        }
        CircleMemberInfo circleMemberInfo = circleMemberInfoService.findById(id);
        if (Objects.equals(CircleMemberInfoEnum.IS_BAN_FALSE.getValue(), isBan)) { //进行禁言
            circleMemberInfo.setIsBan((Integer) CircleMemberInfoEnum.IS_BAN_TRUE.getValue());
            Integer flag = circleMemberInfoService.updateById(circleMemberInfo);
            if (flag == 0) {
                return ResponseResult.buildResult(400, "禁言失败");
            }
            return ResponseResult.buildResult(200, "禁言成功");
        } else { //进行取消禁言
            circleMemberInfo.setIsBan((Integer) CircleMemberInfoEnum.IS_BAN_FALSE.getValue());
            Integer flag = circleMemberInfoService.updateById(circleMemberInfo);
            if (flag == 0) {
                return ResponseResult.buildResult(400, "取消禁言失败");
            }
            return ResponseResult.buildResult(200, "取消禁言成功");
        }
    }

    /**
     * description：批量禁言
     * user 朱正磊
     * time 2019-04-24 16:17
     * @param ids 主键集合(多个id以,进行拼接)
     * @return 返回响应对象
     */
    @RequestMapping("/batchBan")
    @ResponseBody
    public ResponseResult batchBan(String ids) {
        if(!Jurisdiction.buttonJurisdiction(menuUrl, "edit")) {//判断是否有修改权限
            return null;
        }
        Integer flag = circleMemberInfoService.updateBatchBan(ids);
        if (Objects.equals(flag, 0)) {
            return ResponseResult.buildResult(400, "批量禁言失败");
        } else {
            return ResponseResult.buildResult(200, "批量禁言成功");
        }
    }

    /**
     * description：批量取消禁言
     * user 朱正磊
     * time 2019-04-24 16:17
     * @param ids 主键集合(多个id以,进行拼接)
     * @return 返回响应对象
     */
    @RequestMapping("/batchNotBan")
    @ResponseBody
    public ResponseResult batchNotBan(String ids) {
        if(!Jurisdiction.buttonJurisdiction(menuUrl, "edit")) {//判断是否有修改权限
            return null;
        }
        Integer flag = circleMemberInfoService.updateBatchNotBan(ids);
        if (Objects.equals(flag, 0)) {
            return ResponseResult.buildResult(400, "批量取消禁言失败");
        } else {
            return ResponseResult.buildResult(200, "批量取消禁言成功");
        }
    }

}
